package com.compass.doctor.ui.consultation.fragment

import android.os.Bundle
import android.view.View
import androidx.fragment.app.viewModels
import androidx.recyclerview.widget.LinearLayoutManager
import com.compass.doctor.base.BaseVBFragment
import com.compass.doctor.databinding.FragmentBaseListBinding
import com.compass.doctor.ui.consultation.ConsultationDetailsActivity
import com.compass.doctor.ui.consultation.adapter.ConsultationAdapter
import com.compass.doctor.ui.consultation.viewmodel.ConsultationViewModel
import com.compass.framework.constant.KEY_ORDER_TYPE
import com.compass.framework.constant.REFRESH_LAUNCH
import com.compass.framework.decoration.NormalItemDecoration
import com.compass.framework.ext.gone
import com.compass.framework.ext.load
import com.compass.framework.ext.visible
import com.compass.framework.utils.LiveDataBus
import com.compass.framework.utils.dpToPx
import com.scwang.smart.refresh.layout.api.RefreshLayout
import com.scwang.smart.refresh.layout.listener.OnLoadMoreListener
import com.scwang.smart.refresh.layout.listener.OnRefreshListener

/**
 * Description： 会诊中心Fragment
 * Date:2023/12/21 15:35
 */
class ConsultationFragment : BaseVBFragment<FragmentBaseListBinding>(), OnLoadMoreListener,
    OnRefreshListener {

    private val consultationViewModel: ConsultationViewModel by viewModels()
    private lateinit var mAdapter: ConsultationAdapter
    private var consultationId:String=""//会诊id
    private var mPage = 1
    private var orderType: Int = 1//1-我发起的 2-我参与的
    override fun initView(view: View, savedInstanceState: Bundle?) {
        arguments?.let {
            orderType = it.getInt(KEY_ORDER_TYPE)
        }
        initRecyclerView()
    }

    private fun initRecyclerView() {
        mBinding?.refreshLayout?.apply {
            setEnableRefresh(true)
            setEnableLoadMore(true)
            setOnRefreshListener(this@ConsultationFragment)
            setOnLoadMoreListener(this@ConsultationFragment)
        }
        mAdapter = ConsultationAdapter(orderType)
        val dp10 = dpToPx(10)
        mBinding?.recyclerView?.apply {
            layoutManager = LinearLayoutManager(context)
            addItemDecoration(NormalItemDecoration().apply {
                setBounds(left = dp10, top = dp10, right = dp10, bottom = dp10)
                setLastBottom(true)
            })
            adapter = mAdapter
        }


        mAdapter.onItemClickListener = { _: View, position: Int ->
            val item = mAdapter.getItem(position)
            item?.let {
                consultationId=it.id
                ConsultationDetailsActivity.start(requireContext(), consultationId,orderType)
            }
        }

    }


    override fun initData() {
        getPrescribedList()
        LiveDataBus.get().with(REFRESH_LAUNCH, String::class.java).observe(this) {
            getPrescribedList()
        }

        consultationViewModel.consultationLiveData.observe(this) {
            if (mPage == 1) {
                mAdapter.setData(it)
                mBinding?.refreshLayout?.finishRefresh()
                mBinding?.refreshLayout?.setEnableLoadMore(load(it?.size))
                if (it.isNullOrEmpty()) {
                    mBinding?.emptyView?.visible()
                } else {
                    mBinding?.emptyView?.gone()
                }
            } else {
                mBinding?.refreshLayout?.finishLoadMore()
                if (!it.isNullOrEmpty()) {
                    mAdapter.addAll(it)
                } else {
                    mBinding?.refreshLayout?.setEnableLoadMore(false)
                }
            }
        }
    }



    private fun getPrescribedList() {
        consultationViewModel.getConsultationList(mPage, pageSize = 10, orderType)

    }

    override fun onLoadMore(refreshLayout: RefreshLayout) {
        mPage++
        getPrescribedList()
    }

    override fun onRefresh(refreshLayout: RefreshLayout) {
        mPage = 1
        getPrescribedList()
    }
}